/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import model.*;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author shinji
 */
@Named(value = "a_casteManagedBean")
@SessionScoped
public class CasteManagedBean implements Serializable {
    @EJB
    private CasteFacade casteFacade;
    
    private List<Caste> list = new ArrayList<Caste>();
    private Caste obj;
    private String Name;

    public List<Caste> getList() {
        return list;
    }

    public void setList(List<Caste> list) {
        this.list = list;
    }

    public Caste getObj() {
        return obj;
    }

    public void setObj(Caste obj) {
        this.obj = obj;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }
    
    /**
     * Creates a new instance of CasteManagedBean
     */
    public CasteManagedBean() {
    }
    
    /***********************************************/
    
    public void create(){
        try {
            obj = new Caste();
            obj.setCaste(Name);
            
            this.casteFacade.create(obj);
            set_list();
            Name = "";
            
            set_message(FacesMessage.SEVERITY_INFO, "Create Religion", "Create " +obj.getCaste()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Create Religion", "Create " +obj.getCaste()+ " error !!!");
        }
    }
    
    public void edit(){
        try {
            this.casteFacade.edit(obj);
            
            set_message(FacesMessage.SEVERITY_INFO, "Edit Religion", "Edit " +obj.getCaste()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Edit Religion", "Edit " +obj.getCaste()+ " error !!!");
        }
    }
    
    public void remove(Caste c){
        try {
            this.casteFacade.remove(c);
            set_list();
            
            set_message(FacesMessage.SEVERITY_INFO, "Remove Religion", "Remove " +obj.getCaste()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Remove Religion", "Remove " +obj.getCaste()+ " error !!!");
        }
    }
    
    /***********************************************/
    
    public void set_list(){
        try {
            list = this.casteFacade.findAll();
        } catch (Exception e) {
            list = null;
        }
    }
    
    private void set_message(FacesMessage.Severity severity, String title, String message){
        try {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, message));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error message !!!"));
        }
    }
    
}
